package controller;

import sun.misc.Perf;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserServlet extends HttpServlet {

    //重写方法 alt + insert

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数 req 请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");

        System.out.println("servlet");

        System.out.println(username);
        System.out.println(password);
        System.out.println(sex);
        // session 存放值
        req.getSession().setAttribute("username", "lisi");

        //resp 响应
//        resp.getWriter().write(username + " " + password);

        // 页面  转发  重定向
        // request 存放值
        req.setAttribute("username", username);
        req.setAttribute("password", password);
        req.setAttribute("sex", sex);

        List<Person> list = new ArrayList<>();
        Person person = new Person();
        person.setAge(18);
        person.setName("lisi");
        Person person1 = new Person();
        person1.setAge(20);
        person1.setName("zhangsna");
        list.add(person);
        list.add(person1);
        req.setAttribute("list", list);

        // 转发 url不会改变
        req.getRequestDispatcher("user.jsp").forward(req, resp);

        // 重定向 url会改变
//        resp.sendRedirect("user.jsp");

    }


}
